import Vue from "vue";
import Router from "vue-router";
import store from "./store";

import Login from "./views/Login.vue";
import Index from "./views/Index.vue";

const ActicleList = () =>
  import(/* webpackChunkName: "acticleList" */ "./views/acticle/ActicleList.vue");
const ActiclePost = () =>
  import(/* webpackChunkName: "acticlePost" */ "./views/acticle/ActiclePost.vue");
const UserList = () =>
  import(/* webpackChunkName: "userList" */ "./views/user/UserList.vue");

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/index"
    },
    {
      path: "/index",
      component: Index,
      children: [
        {
          path: "/index/acticles",
          name: "acticleList",
          component: ActicleList,
          meta: {
            index: 0,
            breadcrumb: [
              {
                name: "首页",
                link: "/index/acticles"
              },
              {
                name: "文章管理"
              }
            ]
          }
        },
        {
          path: "/index/acticles/:id",
          name: "acticlePost",
          component: ActiclePost,
          meta: {
            index: 0,
            breadcrumb: [
              {
                name: "首页",
                link: "/index/acticles"
              },
              {
                name: "文章管理"
              }
            ]
          }
        },
        {
          path: "/index/users",
          name: "user",
          component: UserList,
          meta: {
            index: 1,
            breadcrumb: [
              {
                name: "首页",
                link: "/index/acticles"
              },
              {
                name: "用户管理"
              }
            ]
          }
        },
        {
          path: "/",
          redirect: "/index/acticles"
        }
      ]
    },
    {
      path: "/login",
      name: "login",
      component: Login
    }
  ]
});

router.beforeEach((to, from, next) => {
  store.state.loading = true;
  if (to.meta && to.meta.breadcrumb) {
    store.commit("BREADCRUMB_COMMIT", to.meta.breadcrumb);
  }
  next();
});
router.afterEach(() => {
  store.state.loading = false;
});

export default router;
